package view;

import java.awt.BorderLayout; 
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;

import javax.swing.JLabel;
import javax.swing.JPanel;

import view.listeners.MenuMouseListener;
import view.listeners.ViewMenuMouseListener;
import view.views.DefaultContentPanelView;
import view.views.NewSubnetContentPanelView;

import view.views.StartUpViewPanel;
import view.views.TreeTablePanelView;
import view.views.TreeTest;




public class GuiBuilder {

	private final ArrayList<DefaultContentPanelView> contentViewList = new ArrayList<DefaultContentPanelView>();

	private StartUpViewPanel startUpViewPanel;
	private MainFrame frame;
	private ToolBar toolBar;
	private final MenuBar menuBar = new MenuBar();

	private ToolBar statusBar;

	private JLabel statusTextLabel;

	public GuiBuilder() {
		
	}
	/**
	 * Inizialisiert die einzelnden Gui-Elemente
	 */
	public void ini(){
		generateMainFrame();
	 
		generateContentView();
		setupMenu();
		setupMenuItems();
		setupToolBar();
		
		startUpViewPanel.load();
		frame.add(startUpViewPanel, BorderLayout.CENTER);
		
		updateFrame();
		
	}
	/**
	 * Erstellt ein ToolBar Objekt dem jede Fenster Componente eigenene ToolBar Einträge hinzufügen kann
	 */
	public void setupToolBar(){
		toolBar =new ToolBar();
		frame.add(toolBar,BorderLayout.NORTH);
		statusBar =new ToolBar();
		MouseAdapter actionListener = new MouseAdapter() {
			
			
			@Override
			public void mousePressed(MouseEvent e) {
				getToolBar().clear();
				getContentViewPanel().load();
				
			}
			
			
		};
		statusBar.addButton("reload.png", actionListener, "reload");
		statusBar.addSeparator();
		statusTextLabel = new JLabel("");
		statusBar.add(statusTextLabel);
		frame.add(statusBar,BorderLayout.SOUTH);
		
	}
	
	public ToolBar getStatusBar() {
		return statusBar;
	}
	public void setupMenu() {
		frame.setJMenuBar(menuBar);
	}

	public void updateFrame() {
		frame.validate();
		frame.repaint();
	}

	private void generateContentView() {

		loadContentPanels();
		startUpViewPanel = new StartUpViewPanel();
		
		startUpViewPanel.setLayout(new GridLayout(1,1));
		
	}

	private void generateMainFrame() {
		frame = new MainFrame("Network Calculator X", 800, 600,"save.png");
		
	}

	

	private void loadContentPanels() {
//		DefaultContentPanelView contentPanel = new DefaultContentPanelView();
//		contentViewList.add(contentPanel);
//		NewSubnetContentPanelView SubnetPanel = new NewSubnetContentPanelView();
//		contentViewList.add(SubnetPanel);

		StartUpViewPanel treeTest = new StartUpViewPanel();
		contentViewList.add(treeTest);
		
//
//		TreeTablePanelView treeTablePanel = new  TreeTablePanelView();
//		contentViewList.add(treeTablePanel);
//
//		TreeTest testPanel = new  TreeTest();
//		contentViewList.add(testPanel);

		

	}
	
	private void setupMenuItems() {
		for (JPanel panel : contentViewList) {
			menuBar.createMenuItem(panel.getName(), menuBar.getNewMenuItem(), new ViewMenuMouseListener());
		}
		MenuMouseListener menuMouseListener = new MenuMouseListener();
		menuBar.createMenuItem("Host-Adresse", menuBar.getNewMenuItem(), menuMouseListener);
		menuBar.createMenuItem("Subnetz", menuBar.getNewMenuItem(), menuMouseListener);
		menuBar.createMenuItem("IP-Adresse", menuBar.getNewMenuItem(), menuMouseListener);
	}

	
	
	public DefaultContentPanelView getContentViewPanel() {
		return startUpViewPanel;
	}

	public MainFrame getFrame() {
		return frame;
	}

	public ToolBar getToolBar() {
		return toolBar;
	}

	public ArrayList<DefaultContentPanelView> getContentViewList() {
		return contentViewList;
	}
	
	public void setStatusText(String statusText) {
		this.statusTextLabel.setText(statusText);
	}

}